package cn.com.anypay.manager.common.utils;

import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * UUID生成器
 * 提供多种UUID生成方式
 */
@Component
public class UuidGenerator {

    /**
     * 生成标准UUID（带连字符）
     * @return UUID字符串，格式：xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
     */
    public static String generateUuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * 生成简化UUID（不带连字符）
     * @return 32位UUID字符串
     */
    public static String generateSimpleUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 生成大写UUID（不带连字符）
     * @return 32位大写UUID字符串
     */
    public static String generateUpperUuid() {
        return generateSimpleUuid().toUpperCase();
    }

    /**
     * 生成短UUID（22位）
     * 使用Base64编码压缩UUID长度
     * @return 22位短UUID字符串
     */
    public static String generateShortUuid() {
        UUID uuid = UUID.randomUUID();
        long mostSigBits = uuid.getMostSignificantBits();
        long leastSigBits = uuid.getLeastSignificantBits();
        
        byte[] bytes = new byte[16];
        for (int i = 0; i < 8; i++) {
            bytes[i] = (byte) (mostSigBits >>> (8 * (7 - i)));
            bytes[i + 8] = (byte) (leastSigBits >>> (8 * (7 - i)));
        }
        
        return java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
    }

    /**
     * 生成时间戳UUID
     * 在UUID前加上时间戳前缀
     * @return 带时间戳的UUID
     */
    public static String generateTimestampUuid() {
        return System.currentTimeMillis() + "_" + generateSimpleUuid();
    }
}